package com.wu01.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wu01.demo.Entity.OrderInfo;
import com.wu01.demo.Entity.PageRequest;
import com.wu01.demo.Entity.PageResult;
import com.wu01.demo.mapper.OrderInfoMapper;
import com.wu01.demo.service.OrderInfoService;
import com.wu01.demo.utits.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Override
    public List<OrderInfo> findAll() {
        return orderInfoMapper.selectAll();
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest,getPageInfo(pageRequest));
    }

    @Override
    public void addOrder(OrderInfo orderInfo) {
        int insert = orderInfoMapper.insert(orderInfo);
        System.out.println("count"+insert);
    }

    @Override
    public PageResult deletePage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest,deletePageInfo(pageRequest));
    }

    @Override
    public PageResult savePage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest,savePageInfo(pageRequest));
    }

    private PageInfo<?> savePageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setId((long) 123);
        orderInfo.setName("dfdfdf");
        int i = orderInfoMapper.addPage(orderInfo);
        System.out.println(i);
        List<OrderInfo> sysMenus = orderInfoMapper.selectPage();
        return new PageInfo<>(sysMenus);
    }

    /**
     * 调用分页插件完成分页
     * @param pageRequest
     * @return
     */
    private PageInfo<?> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        List<OrderInfo> sysMenus = orderInfoMapper.selectPage();
        return new PageInfo<>(sysMenus);
    }

    private PageInfo<?> deletePageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        int i = orderInfoMapper.deleteById(Long.valueOf(19));
        System.out.println(i);
        List<OrderInfo> sysMenus = orderInfoMapper.selectPage();
        return new PageInfo<>(sysMenus);
    }
}